package com.mango.coomb.threads.parameter.util;


import com.mango.coomb.constant.ThreadConst;
import com.mango.coomb.enums.*;
import com.mango.coomb.threads.parameter.ThreadParameter;

public class ThreadParamUpdateUtil {
    public static void updateIsSafe(boolean isSafe) {
        ThreadParameter.setIsSafe(isSafe);
    }

    public static void updateWriterBufferValueArr(int num, Integer value) {
        Integer[] writerBufferValueArr = ThreadParameter.getWriterBufferValueArr();
        writerBufferValueArr[num] = value;
    }

    public static void updateBufferBufferValueArr(int num, Integer value) {
        Integer[] bufferBufferValueArr = ThreadParameter.getBufferBufferValueArr();
        bufferBufferValueArr[num] = value;
    }

    public static void updateReaderPosBufferValueArr(int num, Integer value) {
        Integer[] readerPosBufferValueArr = ThreadParameter.getReaderPosBufferValueArr();
        readerPosBufferValueArr[num] = value;
    }

    public static void updateReaderNegBufferValueArr(int num, Integer value) {
        Integer[] readerNegBufferValueArr = ThreadParameter.getReaderNegBufferValueArr();
        readerNegBufferValueArr[num] = value;
    }

    public static void updateReaderZeroBufferValueArr(int num, Integer value) {
        Integer[] readerZeroBufferValueArr = ThreadParameter.getReaderZeroBufferValueArr();
        readerZeroBufferValueArr[num] = value;
    }

    public static synchronized int searchNull(AreaClass areaClass, Sign sign) {
        if (sign == null) {
            if (areaClass == AreaClass.BUFFER) {
                Integer[] bufferBufferValueArr = ThreadParameter.getBufferBufferValueArr();
                for (int i = 0; i < ThreadParameter.getAreaCount(areaClass, null); i++) {
                    if ((!ThreadParameter.isBufferBufferLockedArr[i]) && bufferBufferValueArr[i] == null) {
                        ThreadParameter.isBufferBufferLockedArr[i] = true;
                        return i;
                    }
                }
                return ThreadConst.NOT_SEARCH;
            } else {
                Integer[] writerBufferValueArr = ThreadParameter.getWriterBufferValueArr();
                for (int i = 0; i < ThreadConst.WRITER_BUFFER_COUNT; i++) {
                    if ((!ThreadParameter.isWriterBufferLockedArr[i]) && writerBufferValueArr[i] == null) {
                        ThreadParameter.isWriterBufferLockedArr[i] = true;
                        return i;
                    }
                }
                return ThreadConst.NOT_SEARCH;
            }
        } else {
            switch (sign) {
                case READER_ZERO:
                    Integer[] readerZeroBufferValueArr = ThreadParameter.getReaderZeroBufferValueArr();
                    for (int i = 0; i < ThreadConst.READER_ZERO_BUFFER_COUNT; i++) {
                        if ((!ThreadParameter.isReaderZeroBufferLockedArr[i]) && readerZeroBufferValueArr[i] == null) {
                            ThreadParameter.isReaderZeroBufferLockedArr[i] = true;
                            return i;
                        }
                    }
                    return ThreadConst.NOT_SEARCH;
                case READER_NEG:
                    Integer[] readerNegBufferValueArr = ThreadParameter.getReaderNegBufferValueArr();
                    for (int i = 0; i < ThreadConst.READER_NEG_BUFFER_COUNT; i++) {
                        if ((!ThreadParameter.isReaderNegBufferLockedArr[i]) && readerNegBufferValueArr[i] == null) {
                            ThreadParameter.isReaderNegBufferLockedArr[i] = true;
                            return i;
                        }
                    }
                    return ThreadConst.NOT_SEARCH;
                case READER_POS:
                    Integer[] readerPosBufferValueArr = ThreadParameter.getReaderPosBufferValueArr();
                    for (int i = 0; i < ThreadConst.READER_POS_BUFFER_COUNT; i++) {
                        if ((!ThreadParameter.isReaderPosBufferLockedArr[i]) && readerPosBufferValueArr[i] == null) {
                            ThreadParameter.isReaderPosBufferLockedArr[i] = true;
                            return i;
                        }
                    }
                    return ThreadConst.NOT_SEARCH;
            }
        }
        return ThreadConst.NOT_SEARCH;
    }

    public static synchronized int searchNotNull(AreaClass areaClass, Sign sign) {
        switch (areaClass) {
            case BUFFER:
                Integer[] bufferBufferValueArr = ThreadParameter.getBufferBufferValueArr();
                if (sign != null) {
                    switch (sign) {
                        case READER_POS:
                            for (int i = 0; i < ThreadParameter.getAreaCount(areaClass, null); i++) {
                                if ((!ThreadParameter.isBufferBufferLockedArr[i]) && bufferBufferValueArr[i] != null
                                        && bufferBufferValueArr[i] > 0) {
                                    ThreadParameter.isBufferBufferLockedArr[i] = true;
                                    return i;
                                }
                            }
                            return ThreadConst.NOT_SEARCH;
                        case READER_NEG:
                            for (int i = 0; i < ThreadParameter.getAreaCount(areaClass, null); i++) {
                                if ((!ThreadParameter.isBufferBufferLockedArr[i]) && bufferBufferValueArr[i] != null
                                        && bufferBufferValueArr[i] < 0) {
                                    ThreadParameter.isBufferBufferLockedArr[i] = true;
                                    return i;
                                }
                            }
                            return ThreadConst.NOT_SEARCH;
                        case READER_ZERO:
                            for (int i = 0; i < ThreadParameter.getAreaCount(areaClass, null); i++) {
                                if ((!ThreadParameter.isBufferBufferLockedArr[i]) && bufferBufferValueArr[i] != null
                                        && bufferBufferValueArr[i] == 0) {
                                    ThreadParameter.isBufferBufferLockedArr[i] = true;
                                    return i;
                                }
                            }
                            return ThreadConst.NOT_SEARCH;
                    }
                } else {
                    for (int i = 0; i < ThreadParameter.getAreaCount(areaClass, null); i++) {
                        if ((!ThreadParameter.isBufferBufferLockedArr[i]) && bufferBufferValueArr[i] != null) {
                            ThreadParameter.isBufferBufferLockedArr[i] = true;
                            return i;
                        }
                    }
                    return ThreadConst.NOT_SEARCH;
                }
            case READER:
                switch (sign) {
                    case READER_NEG:
                        Integer[] readerNegBufferValueArr = ThreadParameter.getReaderNegBufferValueArr();
                        for (int i = 0; i < ThreadConst.READER_NEG_BUFFER_COUNT; i++) {
                            if ((!ThreadParameter.isReaderNegBufferLockedArr[i]) & readerNegBufferValueArr[i] != null) {
                                ThreadParameter.isReaderNegBufferLockedArr[i] = true;
                                return i;
                            }
                        }
                        return ThreadConst.NOT_SEARCH;
                    case READER_POS:
                        Integer[] readerPosBufferValueArr = ThreadParameter.getReaderPosBufferValueArr();
                        for (int i = 0; i < ThreadConst.READER_POS_BUFFER_COUNT; i++) {
                            if ((!ThreadParameter.isReaderPosBufferLockedArr[i]) & readerPosBufferValueArr[i] != null) {
                                ThreadParameter.isReaderPosBufferLockedArr[i] = true;
                                return i;
                            }
                        }
                        return ThreadConst.NOT_SEARCH;
                    case READER_ZERO:
                        Integer[] readerZeroBufferValueArr = ThreadParameter.getReaderZeroBufferValueArr();
                        for (int i = 0; i < ThreadConst.READER_ZERO_BUFFER_COUNT; i++) {
                            if ((!ThreadParameter.isReaderZeroBufferLockedArr[i]) & readerZeroBufferValueArr[i] != null) {
                                ThreadParameter.isReaderZeroBufferLockedArr[i] = true;
                                return i;
                            }
                        }
                        return ThreadConst.NOT_SEARCH;
                }
            case WRITER:
                Integer[] writerBufferValueArr = ThreadParameter.getWriterBufferValueArr();
                for (int i = 0; i < ThreadConst.WRITER_BUFFER_COUNT; i++) {
                    if ((!ThreadParameter.isWriterBufferLockedArr[i]) & writerBufferValueArr[i] != null) {
                        ThreadParameter.isWriterBufferLockedArr[i] = true;
                        return i;
                    }
                }
                return ThreadConst.NOT_SEARCH;
        }
        return ThreadConst.NOT_SEARCH;
    }

    public static void updateModeAndIsHavingData(Modes modes) {
        ThreadParameter.setModeState(modes);
        ThreadParameter.setIsHavingData(Modes.RANDOM != modes);
    }

    public static void updateTargetTotalCount(int targetTotalCount, Sign sign) {
        int[] targetTotalCountArr = ThreadParameter.getTargetTotalCountArr();
        switch (sign) {
            case READER_POS:
                targetTotalCountArr[ThreadConst.TARGET_POS_TOTAL_COUNT_NUM] = targetTotalCount;
                break;
            case READER_NEG:
                targetTotalCountArr[ThreadConst.TARGET_NEG_TOTAL_COUNT_NUM] = targetTotalCount;
                break;
            case READER_ZERO:
                targetTotalCountArr[ThreadConst.TARGET_ZERO_TOTAL_COUNT_NUM] = targetTotalCount;
                break;
        }
    }

    public static void updateBlockingCount(int blockingCount, ThreadClass threadClass, Actions actions) {
        int[] blockingCountArr = ThreadParameter.getBlockingCountArr();
        switch (threadClass) {
            case WRITER:
                switch (actions) {
                    case THREAD_WRITE:
                        blockingCountArr[ThreadConst.WRITER_WRITE_BLOCKING_COUNT_NUM] = blockingCount;
                        break;
                    case THREAD_READ:
                        blockingCountArr[ThreadConst.WRITER_READ_BLOCKING_COUNT_NUM] = blockingCount;
                        break;
                }
                break;
            case READER:
                switch (actions) {
                    case THREAD_WRITE:
                        blockingCountArr[ThreadConst.READER_WRITE_BLOCKING_COUNT_NUM] = blockingCount;
                        break;
                    case THREAD_READ:
                        blockingCountArr[ThreadConst.READER_READ_BLOCKING_COUNT_NUM] = blockingCount;
                        break;
                }
                break;
        }
    }

    public static void updateIdleCount(int idleOrNotCount, AreaClass areaClass, boolean isIdle) {
        int[] idleCountArr = ThreadParameter.getIdleCountArr();
        int[] notIdleCountArr = ThreadParameter.getNotIdleCountArr();
        switch (areaClass) {
            case WRITER:
                if (isIdle) {
                    idleCountArr[ThreadConst.WRITER_IDLE_OR_NOT_COUNT_NUM] = idleOrNotCount;
                } else {
                    notIdleCountArr[ThreadConst.WRITER_IDLE_OR_NOT_COUNT_NUM] = idleOrNotCount;
                }
                break;
            case BUFFER:
                if (isIdle) {
                    idleCountArr[ThreadConst.BUFFER_IDLE_OR_NOT_COUNT_NUM] = idleOrNotCount;
                } else {
                    notIdleCountArr[ThreadConst.BUFFER_IDLE_OR_NOT_COUNT_NUM] = idleOrNotCount;
                }
                break;
            case READER:
                if (isIdle) {
                    idleCountArr[ThreadConst.READER_IDLE_OR_NOT_COUNT_NUM] = idleOrNotCount;
                } else {
                    notIdleCountArr[ThreadConst.READER_IDLE_OR_NOT_COUNT_NUM] = idleOrNotCount;
                }
                break;
        }
    }

    public static void updateAreaCount(int areaCount, AreaClass areaClass, Sign sign) {
        int[] areaCountArr = ThreadParameter.getAreaCountArr();
        if (sign == null) {
            switch (areaClass) {
                case WRITER:
                    areaCountArr[ThreadConst.WRITER_BUFFER_COUNT_NUM] = areaCount;
                    break;
                case BUFFER:
                    areaCountArr[ThreadConst.BUFFER_BUFFER_COUNT_NUM] = areaCount;
                    break;
            }
        } else {
            switch (sign) {
                case READER_NEG:
                    areaCountArr[ThreadConst.READER_BUFFER_NEG_COUNT_NUM] = areaCount;
                    break;
                case READER_POS:
                    areaCountArr[ThreadConst.READER_BUFFER_POS_COUNT_NUM] = areaCount;
                    break;
                case READER_ZERO:
                    areaCountArr[ThreadConst.READER_BUFFER_ZERO_COUNT_NUM] = areaCount;
                    break;
            }
        }
    }

    public static void updateSpeedMultiple(int speedMultiple, ThreadClass threadCLass) {
        int[] speedMultipleArr = ThreadParameter.getSpeedMultipleArr();
        switch (threadCLass) {
            case WRITER:
                speedMultipleArr[ThreadConst.WRITER_SPEED_Multiple_NUM] = speedMultiple;
                break;
            case READER:
                speedMultipleArr[ThreadConst.READER_SPEED_Multiple_NUM] = speedMultiple;
                break;
        }
    }

    public static void updateOperationalStatus(Status status) {
        ThreadParameter.setOperationalStatus(status);
    }

    public static void updateModeState(Modes modesState) {
        ThreadParameter.setModeState(modesState);
    }

    public static void updateIsHavingData(boolean isHavingData) {
        ThreadParameter.setIsHavingData(isHavingData);
    }

    public static void updateTotalTimeSpent(double totalTimeSpent) {
        ThreadParameter.setTotalTimeSpent(totalTimeSpent);
    }

    public static void updateTotalCount(int totalCount) {
        ThreadParameter.setTotalCount(totalCount);
    }

    public static void updateTotalCompletionCount(int totalCompletionCount) {
        ThreadParameter.setTotalCompletionCount(totalCompletionCount);
    }

    public static void updateProcessingSpeedCount(double processingSpeedCount) {
        ThreadParameter.setProcessingSpeedCount(processingSpeedCount);
    }


}
